<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <!--<link rel="stylesheet" href="../plugins/elementui/index.css">-->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>权限管理<small>菜单数据维护</small></h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>权限管理</el-breadcrumb-item>
            <el-breadcrumb-item>菜单数据维护</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-input placeholder="菜单名称" v-model="pagination.queryString" style="width: 200px;" class="filter-item"></el-input>
                <el-button @click="findPage()" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
            </div>
            <template>
                <div>
                    <el-table
                            :data="dataList"
                            style="width: 100%;margin-bottom: 20px;"
                            row-key="id"
                            border
                            :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
                        <el-table-column
                                prop="name"
                                label="菜单名称"
                                width="180"
                                align="center">
                        </el-table-column>
                        <el-table-column
                                prop="linkUrl"
                                label="菜单关联页面"
                                width="180"
                                align="center">
                        </el-table-column>
                        <el-table-column
                                label="菜单图标"
                                width="180"
                                align="center">
                            <template slot-scope="scope">
                                <i class="fa" :class="scope.row.icon"/>
                            </template>
                        </el-table-column>
                        <el-table-column
                                prop="description"
                                label="菜单描述"
                                width="180"
                                align="center">
                        </el-table-column>
                        <el-table-column label="操作" align="center">
                            <template slot-scope="scope">
                                <el-button  type="primary" size="mini" @click="handleCreate(scope.row)">新增子菜单</el-button>
                                <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                                <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                </div>
            </template>
            <!-- 菜单新增标签弹层 -->
            <div class="add-form">
                <el-dialog title="新增菜单" :visible.sync="dialogFormVisible">
                    <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right" label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="菜单名称" prop="name">
                                    <el-input v-model="formData.name"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="菜单图案" prop="icon">
                                    <el-input v-model="formData.icon"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="菜单关联页面" prop="linkUrl">
                                    <el-input v-model="formData.linkUrl"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                            <el-form-item label="菜单描述" prop="description">
                                <el-input v-model="formData.description"/>
                            </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
            <!-- 编辑标签弹层 -->
            <div class="add-form">
                <el-dialog title="编辑检查项" :visible.sync="dialogFormVisible4Edit">
                    <el-form ref="dataEditForm" :model="formData" :rules="rules" label-position="right" label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="菜单名称" prop="name">
                                    <el-input v-model="formData.name"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="菜单图案" prop="icon">
                                    <el-input v-model="formData.icon"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="菜单关联页面" prop="linkUrl">
                                    <el-input v-model="formData.linkUrl"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="菜单描述" prop="description">
                                    <el-input v-model="formData.description"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>
        </div>
    </div>
</div>
</body>
<script src="../js/vue.js"></script>
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
<!--<script src="../plugins/elementui/index.js"></script>-->
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>
<script>
    var vue = new Vue({
        el:"#app",
        data:{
            dataList:[{
                    id: 1, //菜单id
                    name: '一级 1', //菜单名称
                    description: "第一个菜单", //菜单描述
                    level:1, //菜单级别
                    likeUrl:'member.html', //菜单页面
                    icon:'fa-user-md', //菜单图案
                    children: [{
                        id: 4,
                        name: '二级 1-1',
                        description: "第一个二级菜单"
                    }] //子菜单列表
                     // hasChildren: true //是否有子节点
                }],
            formData:{}, //表单项
            pagination: {//分页相关模型数据
                currentPage: 1,//当前页码
                pageSize:10,//每页显示的记录数
                total:0,//总记录数
                queryString:null//查询条件
            },
            dialogFormVisible: false,//增加表单是否可见
            dialogFormVisible4Edit:false,//编辑表单是否可见
            rules: {//校验规则
                name: [{ required: true, message: '项目名称为必填项', trigger: 'blur' }],
                keyword: [{ required: true, message: '项目关键字为必填项', trigger: 'blur' }]
            }
        },
        created(){
            this.findPage();
        },
        methods:{
            //权限不足提示
            showMessage(r){
                if(r == 'Error: Request failed with status code 403'){
                    //权限不足
                    this.$message.error('您暂时还没有该权限');
                    return;
                }else{
                    this.$message.error('未知错误');
                    return;
                }
            },
            //编辑
            handleEdit() {
                axios.post("../menu/edit.do",this.formData)
                    .then(response => {
                        this.$message({
                            type: response.data.flag ?'success':'error',
                            message: response.data.message
                        });
                        if (response.data.flag) {
                            this.dialogFormVisible4Edit = false;
                            this.findPage();
                        }
                    }).catch(error=>{
                    this.showMessage(error);
                    console.log(error);
                })
            },
            //添加
            handleAdd () {
                axios.post("../menu/add.do",this.formData)
                    .then(response => {
                        this.$message({
                            type: response.data.flag ?'success':'error',
                            message: response.data.message
                        });
                        if (response.data.flag) {
                            this.dialogFormVisible = false;
                            // this.pagination.currentPage= 1;
                            this.findPage();
                        }
                    }).catch(error => {
                    this.showMessage(error);
                    console.log(error);
                });
            },
            //分页查询
            findPage() {
                axios.post("../menu/findPage.do",this.pagination)
                    .then(response => {
                        if (response.data.flag){
                            this.dataList = response.data.data.rows;
                            this.pagination.total = response.data.data.total;
                        } else {
                            this.$message(response.data.message);
                        }
                    }).catch(error => {
                    this.showMessage(error);
                    console.log(error);
                })
            },
            // 重置表单
            resetForm() {
                this.formData ={};
            },
            // 弹出添加窗口
            handleCreate(row) {
                this.resetForm();
                if (row != undefined){
                    this.formData.parentMenuId= row.id;
                    this.formData.level= 2;
                } else {
                    this.formData.parentMenuId = null;
                    this.formData.level= 1;
                }
                this.dialogFormVisible = true;
            },
            // 弹出编辑窗口
            handleUpdate(row) {
                axios.get("../menu/findById.do?id=" + row.id)
                    .then(response => {
                        if (response.data.flag) {
                            this.formData = response.data.data;
                            this.dialogFormVisible4Edit = true;
                        } else {
                            this.$message.error(response.data.message);
                        }
                    }).catch(error =>{
                    this.showMessage(error);
                    console.log(error);
                    });
            },
            //切换页码
            handleCurrentChange(currentPage) {
                this.pagination.currentPage = currentPage;
                this.findPage();
            },
            // 删除
            handleDelete(row) {
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    axios.get("../menu/delete.do?id=" + row.id)
                        .then(response => {
                            this.$message({
                                type: response.data.flag ?'success':'error',
                                message: response.data.message
                            });
                            this.pagination.currentPage = 1;
                            this.findPage();
                        }).catch(error => {
                        this.showMessage(error);
                        console.log(error);
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            }
        }
    });
</script>
</html>